package com.ruoyi.receipt.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.receipt.service.ITbKnowledgeManagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.receipt.domain.TbKnowledgeList;
import com.ruoyi.receipt.service.ITbKnowledgeListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 知识点目录管理Controller
 * 
 * @author ruoyi
 * @date 2021-03-27
 */
@RestController
@RequestMapping("/knowledgeList")
public class TbKnowledgeListController extends BaseController
{
    @Autowired
    private ITbKnowledgeListService tbKnowledgeListService;

    @Autowired
    private ITbKnowledgeManagerService tbKnowledgeManagerService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询知识点目录管理列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbKnowledgeList tbKnowledgeList)
    {
        startPage();
        List<TbKnowledgeList> list = tbKnowledgeListService.selectTbKnowledgeListList(tbKnowledgeList);
        return getDataTable(list);
    }

    /**
     * 导出知识点目录管理列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:export')")
    @Log(title = "知识点目录管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbKnowledgeList tbKnowledgeList)
    {
        List<TbKnowledgeList> list = tbKnowledgeListService.selectTbKnowledgeListList(tbKnowledgeList);
        ExcelUtil<TbKnowledgeList> util = new ExcelUtil<TbKnowledgeList>(TbKnowledgeList.class);
        return util.exportExcel(list, "list");
    }

    /**
     * 导出知识点目录审核列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeListAudit:export')")
    @Log(title = "知识点目录管理", businessType = BusinessType.EXPORT)
    @GetMapping("/exportAudit")
    public AjaxResult exportAudit(TbKnowledgeList tbKnowledgeList)
    {
        List<TbKnowledgeList> list = tbKnowledgeListService.selectTbKnowledgeListList(tbKnowledgeList);
        ExcelUtil<TbKnowledgeList> util = new ExcelUtil<TbKnowledgeList>(TbKnowledgeList.class);
        return util.exportExcel(list, "list");
    }

    /**
     * 获取树结构数据
     * @return
     */
    @GetMapping("/getTreeList")
    public List<TbKnowledgeList> getTreeList(){
        //获取所有知识点目录集合
        List<TbKnowledgeList> tbKnowledgeTreeList = tbKnowledgeListService.getTbKnowledgeTreeList();
        return tbKnowledgeTreeList;
    }

    /**
     * 获取知识点目录管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbKnowledgeListService.selectTbKnowledgeListById(id));
    }

    /**
     * 新增知识点目录管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:add')")
    @Log(title = "知识点目录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbKnowledgeList tbKnowledgeList)
    {
        tbKnowledgeList.setId(idWorker.nextId());
        return toAjax(tbKnowledgeListService.insertTbKnowledgeList(tbKnowledgeList));
    }

    /**
     * 验证知识点目录是否重复
     */
    @PostMapping("/duplicateValidation")
    @ResponseBody
    public AjaxResult duplicateValidation(@RequestBody TbKnowledgeList tbKnowledgeList){
        List<TbKnowledgeList> tbKnowledgeLists = tbKnowledgeListService.duplicateValidation(tbKnowledgeList);
        return AjaxResult.success(tbKnowledgeLists.size());
    }

    /**
     * 修改知识点目录管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:edit')")
    @Log(title = "知识点目录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbKnowledgeList tbKnowledgeList)
    {
        return toAjax(tbKnowledgeListService.updateTbKnowledgeList(tbKnowledgeList));
    }

    /**
     * 知识点目录审核
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:edit')")
    @Log(title = "知识点目录审核", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateKnowledgeAudit")
    public AjaxResult updateKnowledgeAudit(@RequestBody TbKnowledgeList tbKnowledgeList)
    {
        return toAjax(tbKnowledgeListService.updateKnowledgeAudit(tbKnowledgeList));
    }

    /**
     * 删除知识点目录管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:remove')")
    @Log(title = "知识点目录管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //先去判断目录下是否有知识点 没有知识点的才可以删除
        List<Map<String, Object>> maps = tbKnowledgeListService.selectKnowledgeByKnowledgeIds(ids);
        if (maps.size() > 0){
            AjaxResult success = AjaxResult.success(maps);
            return AjaxResult.success(maps);
        }else {
            return toAjax(tbKnowledgeListService.deleteTbKnowledgeListByIds(ids));
        }
    }

    /**
     * 删除知识点目录管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeList:remove')")
    @Log(title = "知识点目录管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/delListLogic/{ids}")
    public AjaxResult delListLogic(@PathVariable Long[] ids)
    {
        //先去判断目录下是否有知识点 没有知识点的才可以删除
        List<Map<String, Object>> maps = tbKnowledgeListService.selectKnowledgeByKnowledgeIds(ids);
        if (maps.size() > 0){
            return AjaxResult.success(maps.size());
        }else {
            return toAjax(tbKnowledgeListService.delListLogic(ids));
        }
    }

}
